const path = require('path')
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const VueLoaderPlugin = require('vue-loader/lib/plugin')
const miniCssExtractPlugin = require('mini-css-extract-plugin')

module.exports = {
  mode: 'none',
  entry: './src/main.js',
  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, './dist'),
  },
  module: {
    rules: [
      {
        test: /\.vue$/,
        loader: 'vue-loader',
      },
      {
        test: /\.js$/,
        loader: 'babel-loader',
        exclude: /node_modules/,
      },
      {
        test: /.css$/,
        use: [
          {
            loader: miniCssExtractPlugin.loader,
          },
          'css-loader',
        ],
      },
      {
        test: /\.less$/,
        use: ['vue-style-loader', 'css-loader', 'less-loader'],
      },
      {
        test: /\.(png|jge?g|gif)$/i,
        use: {
          loader: 'url-loader',
          options: {
            limit: 1024 * 10, // 10kb
            name: '[name].[hash].[ext]',
            esModule: false,
          },
        },
      },
    ],
  },
  plugins: [
    new HtmlWebpackPlugin({
      title: 'vue app base',
      template: path.join(__dirname, './public/index.html'), // 指定模板文件路径
      filename: 'index.html', // 设置生成的内存页面的名称
      enject: true,
    }),
    new miniCssExtractPlugin({
      filename: '[name]_[contenthash:8].css',
    }),
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({
      // 值要求的是一个代码片段
      BASE_URL:
        process.env.NODE_ENV === 'production'
          ? JSON.stringify('./')
          : JSON.stringify('/'),
    }),
  ],
}
